﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;

namespace Yuanlin.Business
{
    public class HelpCenterEditControl : YuanlinUserControl,
        IReplyConverter<HelpCenterReply, GetOperation>
    {
        protected ValuedTextBox leftMenuNameValuedTextBox;
        protected ValuedEditor descriptionValuedEditor;
        protected ValuedTextBox orderByValuedTextBox;

        protected IButton SaveButton;
        protected IButton BackButton;
        private HelpCenter helpCenter;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            requestBinders.Add(RequestHelper.BuildGetHelpCenterRequest(this));
        }

        protected override void OnLoad(EventArgs e)
        {
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        private void Bind()
        {
            if (helpCenter != null)
            {
                this.leftMenuNameValuedTextBox.Value = helpCenter.LeftMenuName.Value;
                this.descriptionValuedEditor.Value = helpCenter.Body.Value;
                this.orderByValuedTextBox.Value = helpCenter.OrderBy.Value.ToString();
            }   
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect(SiteUrls.Instance().GetHelpCenterListUrl());
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            if (helpCenter != null)
            {
                helpCenter.LeftMenuName.Value = this.leftMenuNameValuedTextBox.Value;
                helpCenter.Body.Value = this.descriptionValuedEditor.Value;
                helpCenter.OrderBy.Value = Int32.Parse(this.orderByValuedTextBox.Value.Trim());

                if (BusinessManager.UpdateHelpCenter(helpCenter) == -2)
                {
                    Globals.ShowMessage(this.Page, "修改失败", "此信息已经存在！");
                    return;
                }
            }
            Page.Response.Redirect(SiteUrls.Instance().GetHelpCenterListUrl());
        }

        #region IReplyConverter<HelpCenterReply,GetOperation> Members

        public void ConvertReply(HelpCenterReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                helpCenter = reply.Entity as HelpCenter;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}